package ltd.newbee.mall.controller.admin;

import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.Hall;
import ltd.newbee.mall.service.HallService;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping(value = "/Hall")
public class HallController {

    @Resource
    HallService hallService;


    @GetMapping("/Hall")
    public String goodsPage(HttpServletRequest request) {
        request.setAttribute("path", "Hall"); //导入工具栏
        return "admin/film_hall";
    }
    //反显数据
    @RequestMapping(value="/selectAll", method = RequestMethod.GET)
    @ResponseBody
    public Result selectAll(@RequestParam Map<String, Object> params){
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(hallService.selectAll(pageUtil));
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody Hall hall) {
        if (StringUtils.isEmpty(hall.getHallName())
                || StringUtils.isEmpty(hall.getColumns())
                || StringUtils.isEmpty(hall.getRow())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result=hallService.saveHall(hall);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody Hall hall) {
        if (StringUtils.isEmpty(hall.getHallId())
                ||StringUtils.isEmpty(hall.getHallName())
                || StringUtils.isEmpty(hall.getColumns())
                || StringUtils.isEmpty(hall.getRow())) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result=hallService.updateHall(hall);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 分类删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (hallService.deleteHall(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }


    @RequestMapping(value = "/Halls")
    public String delete() {
        return "film/index";
    }
    //影厅详情展示
    @GetMapping("/into/{id}")
    public String into(HttpServletRequest request,@PathVariable("id") Long id,Model model) {
        System.out.println("id:"+id);
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String one = simpleDateFormat.format(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String two = simpleDateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String three = simpleDateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String four  = simpleDateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String five = simpleDateFormat.format(calendar.getTime());
        model.addAttribute("id",id);
        model.addAttribute("one",one);
//        request.setAttribute("one", one); //当前时间
        request.setAttribute("two", two); //后一天
        request.setAttribute("three", three); //二天
        request.setAttribute("four", four); //三天
        request.setAttribute("five", five); //四天
        request.setAttribute("path", "Hall"); //导入工具栏
        return "admin/film_Hall_Info";
    }



    //反显数据
    @RequestMapping(value="/HallTime", method = RequestMethod.GET)
    @ResponseBody
    public Result HallTime(@RequestParam Map<String, Object> params) throws ParseException {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit")) || StringUtils.isEmpty(params.get("nowtime"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        String nowtime= String.valueOf(params.get("nowtime"));
        String hallId= String.valueOf(params.get("hallId"));
        return ResultGenerator.genSuccessResult(hallService.selectHall(pageUtil,nowtime,hallId));
    }



}
